package client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.*;

import sharedspace.*;

import messages.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;

import java.util.List;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
/**
 * @This Class give the teacher the option to choose what question to edit
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Teacher_Edit_Question extends GUI_AbstractMainMenu{
	private JLabel lblChooseQuestion = null;
	private JScrollPane qScroller = null;
	private JTable qJTable = null;
	private List<Question> qList = null;
	private JButton qBtn = null;
	public JButton btnBack = null;

	Client_GUI ms=null;

	public GUI_Teacher_Edit_Question(Client_GUI MS) {
		super(MainMenuType.TEACHER,MS);
		this.ms = MS;
		this.initiate();
		greetMsg.setText("Edit Question");
		mainMenu.setText("Teacher");

	}
	
	public void initiate()
	{
		this.setSize(1000, 700);	
		this.setLayout(null);
		background.add(getbtnBack(),null);
		background.add(getQuestionTable(),null);
		background.add(getEditButton());
		lblChooseQuestion = new JLabel("Choose an Exam:");
		lblChooseQuestion.setForeground(Color.BLUE);
		lblChooseQuestion.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 16));
		lblChooseQuestion.setBounds(349, 120, 147, 34);
		background.add(lblChooseQuestion);


	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}
	
	private JButton getEditButton()
	{
		if (qBtn == null)
		{
			qBtn = new JButton("Edit Question");	
			qBtn.setBounds(400, 600, 150, 30);
			qBtn.setVisible(true);
			qBtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if (qJTable.getSelectedRow() == -1) JOptionPane.showMessageDialog(null,"Please select a question");
					else{
					ms.teq2=new GUI_Teacher_Edit_Question2(ms,qList.get(qJTable.getSelectedRow()));
					ms.setContentPane(ms.teq2);
					ms.teq2.btnBack.addActionListener(new java.awt.event.ActionListener() {
						public void actionPerformed(java.awt.event.ActionEvent e) {
							ms.teq=new GUI_Teacher_Edit_Question(ms);
							ms.setContentPane(ms.teq);
							ms.teq.btnBack.addActionListener(new java.awt.event.ActionListener() {
								public void actionPerformed(java.awt.event.ActionEvent e) {
									ms.setContentPane(ms.tchr);
								}});
							
						}});
					}
				}
			});
		}
		
		return qBtn;
	}
	
	@SuppressWarnings("unchecked")
	private JScrollPane getQuestionTable()
	{
		if (qJTable == null)
		{
			MessageQuestionListByTeacherID msgGql=new MessageQuestionListByTeacherID(ms.lg.getUser().getUserID());
			try {
				ms.client.sendToServer(msgGql);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			qList = (ArrayList<Question>)ms.client.GetMessage();
			String columnNames[] = {"ID","Subject","Question","Answer 1","Answer 2","Answer3", "Answer 4", "Corret Answer", "Question Comment"};
			String tableModel[][] = new String[qList.size()][Question.NUM_OF_COLUMNS-1];
			for (int i=0; i<qList.size(); i++){
				tableModel[i][0] = qList.get(i).getQuestionID();
				tableModel[i][1] = qList.get(i).getQuestionSubject();
				tableModel[i][2] = (qList.get(i).getQuestion());
				tableModel[i][3] = "" + qList.get(i).getAnswers()[0];
				tableModel[i][4] = "" + qList.get(i).getAnswers()[1];
				tableModel[i][5] = "" + qList.get(i).getAnswers()[2];
				tableModel[i][6] = "" + qList.get(i).getAnswers()[3];
				tableModel[i][7] = "" + qList.get(i).getCorrectAnswer();
				tableModel[i][8] = qList.get(i).getQuestionCommet();
			}
			qJTable = new JTable(tableModel, columnNames){
				   public boolean isCellEditable(int rowIndex, int colIndex) {
				   return false; //Disallow the editing of any cell
				   }
				   };		 
			qJTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
			qJTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			qJTable.getTableHeader().setReorderingAllowed(false);
			TableColumn columnCorrect=qJTable.getColumnModel().getColumn(1);
			  columnCorrect.setPreferredWidth(WIDTH*15);
			if (qList.size() > 0) qJTable.setRowSelectionInterval(0, 0);
			qScroller= new JScrollPane(qJTable);
			qScroller.setBounds(20, 150, 950, 400);
		}
			
		
		return qScroller;
	}
}
